package com.summer.main;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class WorkerThread {

	public static void main(String[] args) {
		Helper helper = new Helper();
		helper.init();
		helper.submit("任务1");
		try {
			Thread.sleep(10000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		helper.submit("任务2");
		helper.submit("任务3");
		helper.submit("任务4");
		helper.submit("任务5");
		helper.submit("任务6");
	}
	
	static class Helper {
		private final BlockingQueue<String> workerQueue = new ArrayBlockingQueue<String>(1);
		
		private final Thread workerThread = new Thread(){

			@Override
			public void run() {
				String task = null;
				while(true){
					try{
						task = workerQueue.take();
					}catch(InterruptedException ex){
						break;
					}
					System.out.println(doProcess(task));
				}
			}
			
		};
		
		public void init(){
			workerThread.start();
		}
		
		protected String doProcess(String task) {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return task + "->Process";
		}
		
		public void submit(String task){
			try{
				workerQueue.put(task);
				System.out.println(task+"->add");
			}catch(InterruptedException ex){
				Thread.currentThread().interrupt();
			}
		}
		
	}
	
}
